package tech.yangzhijie.basic.knowledge.other;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class CopyOnWriteTest {

    public static void main(String[] args) {
        CopyOnWriteDemo copyOnWriteDemo = new CopyOnWriteDemo();

        for(int i = 0;i< 10;i++){
            new Thread(copyOnWriteDemo).start();
        }
    }
}

class CopyOnWriteDemo implements Runnable{

    //private static List<String> list = Collections.synchronizedList(new ArrayList<>());
    private static List<String> list = new CopyOnWriteArrayList<>();


    static {
        list.add("AA");
        list.add("BB");
        list.add("CC");
    }

    @Override
    public void run() {
        for (String str :list) {
            System.out.println(str);
            list.add("DD");
        }
    }
}